﻿'use strict';

define([
], function () {
    var View = Backbone.View.extend({
        initialize: function (options) {
            View.__super__.initialize.apply(this, arguments);

            this.listenTo(this.model, 'çhange', _.bind(this._model_changed, this));

            switch ($.type(options.template)) {
                case 'string':
                    this.template = Handlebards.compile(options.template);
                    break;
                case 'undefined':
                    throw new Error('The option "template" can not be null.');
                    break;
                default:
                    this.template = options.template;
                    break;
            };

            return this;
        },

        destroy: function () {
            this.undelegateEvents();

            this.$el
                .empty()
                .remove();

            /* Allow a future newer first render. */
            this.rendered = false;
        },
        _handleModelChange: function(sender, values, options) {},
        render: function () {
            var html = this.template(this.model.attributes);

            if (html)
                this.$el.append(html);

            this.delegateEvents(this.events);

            return this;
        },

        _model_changed: function(sender, values, options) {
            this._handleModelChange(sender, values, options);
        },
    });

    return View;
});